/**
 * cookies处理工具：目前用于皮肤切换
 */

/* 设置cookie值 */
function setCookie(name, value, days) {
	var exp = new Date();
	exp.setTime(exp.getTime() + days*24*60*60*1000);
	document.cookie = name+"="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/";
}

/* 获取cookie值 */
function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)){
		return unescape(arr[2]);
	} else {
		return null;
	}
}

/* 删除cookie值 */
function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime()-1000);
	var value = getCookie(name);
	if(value!=null) {
		document.cookie = name+"="+ value + ";expires=" + exp.toGMTString();
	}
}

/* 设置皮肤 */
function __CHANGE_SKIN__(skinName, days){
	setCookie("nowskin", skinName, days);
	fnFrameChangeSkin(skinName);
}

/* 皮肤切换时自动调用该JS */
function fnFrameChangeSkin(skinName) {
	var thisskin = getCookie("nowskin"); // 获取当前皮肤
	
	if(typeof(skinName)!="undefined" && skinName!=null && skinName!="") {
		thisskin = skinName;
	}
	
	if(thisskin==null){
		thisskin = "default";
	}
	
	var links = document.getElementsByTagName("link");
	var linkStyleObj = null;
	var linkhref = "";
	var oldskin = ""
	for(var i=0;i<links.length;i++){
		linkStyleObj = links[i];
		linkhref = linkStyleObj.href;
		if(typeof(linkhref)!="undefined" && linkhref!=null && linkhref!="" && linkhref.indexOf("resources-")!=-1){
			if(linkhref.indexOf("css/ext-all.css")!=-1){
				oldskin = linkhref.substring(linkhref.indexOf("resources-"), linkhref.indexOf("/css/ext-all.css"));
			}else if(linkhref.indexOf("/css/wonders.css")!=-1){
				oldskin = linkhref.substring(linkhref.indexOf("resources-"), linkhref.indexOf("/css/wonders.css"));
			}
			linkhref = linkhref.replace(oldskin,"resources-"+thisskin);
			linkStyleObj.href = linkhref;
		}
	}
	
	/* frame嵌套 */
	var iframes = document.getElementsByTagName("iframe");
	for(var i=0;i<iframes.length;i++){
		try{
			iframes[i].contentWindow.fnFrameChangeSkin(skinName);
		}catch(e){
			//--
		}
	}
}
